/** 
 * @Description: 文件描述
 * @Author: 程前
 * @Date: 2024-12-24 16:29:54
 * @LastEditors: 程前
 * @LastEditTime: 2024-12-25 17:35:08
 */


import { UrlTemplateImageryProvider } from "cesium";
import GCJ02TilingScheme from "../tiling/GCJ02TilingScheme";
import { replacePlaceholders } from "@/utils";

/**
 * lang: zh_cn设置中文，en设置英文
 * size: 基本无作用【不写】
 * scl: 1含注记、2不含注记
 * style: 6为影像图、7为矢量路网、8为影像路网
 */
const LAYER_TILE_URL: any = {
    img: `//webst{s}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}`,
    elec: `//webrd{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}`,
    cva: `//webst{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}`,
    traffic: `//webst{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=6&x={x}&y={y}&z={z}`,
};

/**
 * @description 处理UrlTemplateImageryProvider所需要的参数
 * @param {String} options 原始的options参数
 * @return {UrlTemplateImageryProvider.ConstructorOptions}
 */
const getOptions = (
    options: any
): UrlTemplateImageryProvider.ConstructorOptions => {
    options.crs = options.crs || "WGS84"; // WGS84 | GCJ02
    const urlTemplate =
        options.url || LAYER_TILE_URL[options?.tileType] || LAYER_TILE_URL.img;
    const url: string = replacePlaceholders(urlTemplate, options);

    let LayerOption: UrlTemplateImageryProvider.ConstructorOptions = {
        url: url,
        subdomains: options.subdomains || ["01", "02", "03", "04"],
        maximumLevel: options.maximumLevel || 18,
        minimumLevel: options.minimumLevel || 1,
    };
    if (options.crs === "WGS84") {
        LayerOption.tilingScheme = new GCJ02TilingScheme();
    }
    return LayerOption;
};

/**
 * 创建高德图层provider
 * @param opts 
 * @returns 
 */
export const createAMapImageryProvider = (opts: any): UrlTemplateImageryProvider => {
    const provideOptions: UrlTemplateImageryProvider.ConstructorOptions =
        getOptions(opts);
    return new UrlTemplateImageryProvider(provideOptions);
};
